package com.angel.web.health;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.util.JsonUtil;
import com.angel.core.util.Pager;
import com.angel.health.BuzServicePackages;
import com.angel.health.BuzServicePackagesProject;
import com.angel.health.SysDict;
import com.angel.provider.health.BuzServicePackagesProvider;
import com.angel.provider.sys.sysManagerDictProvider;

/**
 * 服务套餐
 * @author hanlinxiu
 *
 */
@Controller
@RequestMapping("servicePackages")
public class BuzServicePackagesController {
	
	@Autowired
	private BuzServicePackagesProvider buzServicePackagesSerivce;
	@Autowired
	private sysManagerDictProvider smdProvider;

	@ResponseBody
	@RequestMapping("index")
	public String index(HttpServletRequest request,@ModelAttribute("buzServicePackages")BuzServicePackages buzServicePackages){
//		List<Map<String, Object>> list = buzServicePackagesSerivce.findAll(buzServicePackages);
		List<BuzServicePackages> list = buzServicePackagesSerivce.findServicePackagesAll(buzServicePackages);
		for(BuzServicePackages servicePackages : list){
			List<BuzServicePackagesProject> projectList = servicePackages.getProjectList();
			for(BuzServicePackagesProject p : projectList){
				
				System.out.println(p.getId()+"====");
			}
		}
		return JsonUtil.toJson(list);
	}
	
	/**
	 * 服务套餐首页
	 * 
	 * @param request
	 * @param buzPackages
	 * @param pager
	 * @return
	 */
	@RequestMapping("/servicePackage")
	public String servicePackage(HttpServletRequest request,@ModelAttribute("buzServicePackages")BuzServicePackages buzServicePackages,Pager pager){
		
		buzServicePackages.setEnable(1);
		
		pager = buzServicePackagesSerivce.findAll(buzServicePackages, pager);
		
		request.setAttribute("pager", pager);
		
		return "institutions/service_package";
	}
	
	/**
	 * 新增页面
	 * 
	 * @param id
	 * @param from
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/toAddServicePackage")
	public String toAddServicePackage(BuzServicePackages buzServicePackages, String from, ModelMap modelMap){
		
		
		if(buzServicePackages.getId() != null){
			buzServicePackages = buzServicePackagesSerivce.findById(buzServicePackages);
		}
		SysDict sd = new SysDict();
		sd.setLabel("servicePackage");
		List<Map<String, Object>> sdList = smdProvider.findAll(sd);
		modelMap.put("from", from);
		modelMap.put("data", buzServicePackages);
		modelMap.put("sd",sdList);
		
		return "institutions/service_package_add";
	}
	
	
	/**
	 * 新增，更新服务套餐
	 * 
	 * @param buzInstitutions
	 * @return  
	 */
	@RequestMapping("/addServicePackage")
	@ResponseBody
	public Map<String, Object> addServicePackage(@ModelAttribute("user") String user, @ModelAttribute("buzServicePackages")BuzServicePackages buzServicePackages){
		
		Map<String, Object> map = new HashMap<>();
		
		if(buzServicePackages.getName()==null || buzServicePackages.getName().trim().equals("")){
			map.put("status", "error");
			map.put("msg", "服务套餐名称不能为空!");
			return map;
		}
		
		if(buzServicePackages.getId() == null){
			buzServicePackages.setCreateBy(user);
			buzServicePackagesSerivce.insert(buzServicePackages);
		}else{
			buzServicePackages.setUpdateBy(user);
			buzServicePackagesSerivce.updateById(buzServicePackages);
		}
			
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
	
	
	/**
	 * 删除体检套餐
	 * 
	 * @param buzException
	 * @return
	 */
	@RequestMapping("/deleteServicePackage")
	@ResponseBody
	public Map<String, Object> deleteServicePackage(@ModelAttribute("buzServicePackages")BuzServicePackages buzServicePackages){
		Map<String, Object> map = new HashMap<>();
		
		buzServicePackagesSerivce.delById(buzServicePackages);
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
	
	
	/**
	 * 批量删除
	 *  
	 * @param checks
	 * @return
	 */
	@RequestMapping("/deleteServicePackageBatch")
	@ResponseBody
	public Map<String,Object> deleteServicePackageBatch(@RequestParam("checks[]")List<Integer> checks){
		
		Map<String, Object> map = new HashMap<>();
		
		buzServicePackagesSerivce.deleteByKeysSelective(checks);
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
	
	
	@RequestMapping("addServicePackages")
	public String addServicePackages(@ModelAttribute("buzServicePackages")BuzServicePackages buzServicePackages){
		buzServicePackages.setName("测试批量1");
		buzServicePackages.setCreateBy("admin");
		BuzServicePackagesProject bspp = new BuzServicePackagesProject();
		bspp.setServiceContent("服务1");
		bspp.setPackagesId(1);
		
		BuzServicePackagesProject bspp1 = new BuzServicePackagesProject();
		bspp1.setServiceContent("服务2");
		List<BuzServicePackagesProject> list = new ArrayList<BuzServicePackagesProject>();
		list.add(bspp);
		list.add(bspp1);
		
		buzServicePackages.setProjectList(list);
		
		int insert = buzServicePackagesSerivce.insert(buzServicePackages);
		return JsonUtil.toJson(insert);
	}
	
	@RequestMapping("editServicePackages")
	public String editServicePackages(@ModelAttribute("buzServicePackages")BuzServicePackages buzServicePackages){
		int update = buzServicePackagesSerivce.updateById(buzServicePackages);
		return JsonUtil.toJson(update);
	}
}
